package com.huanglj.himalaya.views;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huanglj.himalaya.R;
import com.huanglj.himalaya.adapters.PlayListAdapter;
import com.huanglj.himalaya.base.BaseApplication;
import com.huanglj.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;

import java.util.ArrayList;
import java.util.List;

public class PobWindow extends PopupWindow implements View.OnClickListener {

    private static final String TAG = "PobWindow";

    private final View mView;
    private PlayListAdapter mAdapter;
    private List<Track> mList = new ArrayList<>();
    private RecyclerView mShowListView;
    private LinearLayout mPobPlayMode;
    private OnPlayListListener mListener;
    private ImageView mImageMode;
    private TextView mTextMode;
    private View mOrderList;
    private TextView mOrderText;
    private ImageView mOrderImage;

    public PobWindow(Context context) {
        //设置宽高
        super(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        //允许触摸外面
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        //加载view
        mView = LayoutInflater.from(BaseApplication.getContext()).inflate(R.layout.play__track_list, null);
        //设置内容
        setContentView(mView);
        setAnimationStyle(R.style.pop_animation);
        initView();
    }

    private void initView() {
        Button closeBtn = mView.findViewById(R.id.close_player_list);
        closeBtn.setOnClickListener(this);
        mShowListView = mView.findViewById(R.id.show_play_list_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mView.getContext());
        mShowListView.setLayoutManager(layoutManager);
        mAdapter = new PlayListAdapter();
        mShowListView.setAdapter(mAdapter);
        mPobPlayMode = mView.findViewById(R.id.pobWindow_play_mode);
        mPobPlayMode.setOnClickListener(this);
        mImageMode = mView.findViewById(R.id.play_mode_switch_btn);
        mTextMode = mView.findViewById(R.id.switch_btn);
        mOrderText = mView.findViewById(R.id.order_list_text);
        mOrderImage = mView.findViewById(R.id.order_image);
        mOrderList = mView.findViewById(R.id.order_list);
        mOrderList.setOnClickListener(this);


    }

    public void setListData(List<Track> trackList) {
        mAdapter.setListData(trackList);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close_player_list:
                dismiss();
                break;
            case R.id.pobWindow_play_mode:
                LogUtil.d(TAG,"切换模式被点击了");
                mListener.onPlayMode();
                break;
            case R.id.order_list:
                mListener.onPlayOrderList();
                break;
        }
    }

    public void setListPosition(int position){
        mAdapter.setPosition(position);
        mShowListView.scrollToPosition(position);

    }
    //暴露一个设置点击事件的接口
    public void setOnItemClickListener(OnItemClickListener listener){
        mAdapter.setOnItemClick(listener);
    }
    //改变播放模式
    public void updateModeChange(XmPlayListControl.PlayMode mode) {
        switch (mode) {
            case PLAY_MODEL_LIST:
                mImageMode.setImageResource(R.mipmap.sort_descending_press);
                mTextMode.setText("顺序播放");
                break;
            case PLAY_MODEL_LIST_LOOP:
                mImageMode.setImageResource(R.mipmap.play_mode_loop_press);
                mTextMode.setText("歌单循环");
                break;
            case PLAY_MODEL_RANDOM:
                mImageMode.setImageResource(R.mipmap.play_mode_random_press);
                mTextMode.setText("随机播放");
                break;
            case PLAY_MODEL_SINGLE_LOOP:
                mImageMode.setImageResource(R.mipmap.play_mode_loop_one_press);
                mTextMode.setText("单曲循环");
                break;
            default:
                break;
        }
    }
    public void updateOrderChange(boolean order){
        mOrderText.setText(order ? "顺序" : "逆序");
        mOrderImage.setImageResource(order ? R.mipmap.sort_descending_normal : R.mipmap.sort_ascending_press);
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    //暴露一个设置切换模式的接口

    public void setOnPlayListener(OnPlayListListener listener){
        mListener = listener;
    }
    public interface OnPlayListListener {
        void onPlayMode();
        void onPlayOrderList();
    }
}
